from app.utils.scputil import SCPClient


class SCPClientFlask(object):
    def __init__(self, app=None):
        self._scp_client = None
        self.app = app
        if app is not None:
            self.init_app(app)

    def init_app(self, app):
        # SCP_USE = app.config.get("SCP_USE")
        SCP_IPS = app.config.get("SCP_IPS")
        SCP_PORT = app.config.get("SCP_PORT")
        SCP_USER = app.config.get("SCP_USER")
        SCP_PASSWORD = app.config.get("SCP_PASSWORD")
        self._scp_client = SCPClient(SCP_USER,SCP_PASSWORD,SCP_PORT,SCP_IPS)
        if not hasattr(app, "extensions"):
            app.extensions = {}

        app.extensions["scp_client"] = self

    def upload(self, object_name, object_path):
        if self._scp_client is not None:
            return self._scp_client.upload(object_name, object_path)
        else:
            raise AssertionError("SCPClient 未出初始化")

    def download(self, object_name, download_path):
        if self._scp_client is not None:
            return self._scp_client.download(object_name, download_path)
        else:
            raise AssertionError("SCPClient 未出初始化")

    def close(self):
        if self._scp_client is not None:
            return self._scp_client.close()
        else:
            raise AssertionError("OSSClient 未出初始化")

